package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Disease;
import com.ruoyi.system.service.IDiseaseService;
import com.ruoyi.system.service.impl.DiseaseCategoryServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 疾病信息Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "疾病信息模块")
@RestController
@RequestMapping("/system/Disease")
public class DiseaseController extends BaseController {
    @Autowired
    private IDiseaseService diseaseService;

    private class Wrap implements Serializable {
        Disease Disease;
        String DiseaseTypeId;

        @JsonProperty("Disease")
        public com.ruoyi.system.domain.Disease getDisease() {
            return Disease;
        }

        @JsonProperty("DiseaseTypeId")
        public String getDiseaseTypeId() {
            return DiseaseTypeId;
        }
    }

    @Autowired
    DiseaseCategoryServiceImpl diseaseCategoryService;

    private Wrap convert(Disease origin) {
        Wrap tempWrap = new Wrap();
        tempWrap.DiseaseTypeId = diseaseCategoryService.getById(origin.getDiseaseTypeId()).getDiseaseName();
        tempWrap.Disease = origin;
        return tempWrap;
    }


    /**
     * 查询疾病信息列表
     */
    @ApiOperation(value = "查询疾病信息列表")
    @PreAuthorize("@ss.hasPermi('system:Disease:list')")
    @GetMapping("/list")
    public Page<Wrap> list(Disease entity, Page<Disease> page) {
        LambdaQueryWrapper<Disease> lambdaQueryWrapper = new LambdaQueryWrapper<Disease>();
        Page<Disease> old = diseaseService.page(page, lambdaQueryWrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps = new ArrayList<>();
        List<Disease> object = old.getRecords();
        for (Disease item : object) {
            wraps.add(convert(item));
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPage(pageWrap, old);
        return pageWrap;

    }

    /**
     * 导出疾病信息列表
     */
    @ApiOperation(value = "导出疾病信息列表")
    @PreAuthorize("@ss.hasPermi('system:Disease:export')")
    @Log(title = "疾病信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Disease disease) {
        LambdaQueryWrapper<Disease> queryWrapper = new LambdaQueryWrapper<>();
        List<Disease> list = diseaseService.list(queryWrapper);
        ExcelUtil<Disease> util = new ExcelUtil<Disease>(Disease.class);
        util.exportExcel(response, list, "疾病信息数据");
    }


    /**
     * 获取疾病信息详细信息
     */
    @ApiOperation(value = "获取疾病信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:Disease:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(diseaseService.getById(id));
    }

    /**
     * 新增疾病信息
     */
    @ApiOperation(value = "新增疾病信息")
    @PreAuthorize("@ss.hasPermi('system:Disease:add')")
    @Log(title = "疾病信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Disease disease) {
        return toAjax(diseaseService.save(disease));
    }

    /**
     * 修改疾病信息
     */
    @ApiOperation(value = "修改疾病信息")
    @PreAuthorize("@ss.hasPermi('system:Disease:edit')")
    @Log(title = "疾病信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Disease disease) {
        return toAjax(diseaseService.updateById(disease));
    }


    /**
     * 删除疾病信息
     */
    @ApiOperation(value = "删除疾病信息")
    @PreAuthorize("@ss.hasPermi('system:Disease:remove')")
    @Log(title = "疾病信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(diseaseService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
